<?php

namespace app\admin\model;

use think\Model;

/**
 * 用户会员模型
 * @package app\admin\model
 */
class UsersMember extends Model
{
    protected $name = 'users_member_cp';

    /**
     * 会员统计
     * @param array $where
     * @return int
     */
    public static function countUsersMember($where = [])
    {
        return self::where($where)->where('end_time', '>', time())->count();
    }

    /**
     * 会员列表
     * @param $where
     * @param $page
     * @param $pagesize
     * @return array
     */
    public static function getUsersMemberList($where, $page, $pagesize)
    {
        $data = self::alias('m')
            ->join('users u', 'm.uid = u.id', 'left')
            ->field('m.*,u.nickname,u.headimg')
            ->where($where)
            ->where('m.end_time', '>', time())
            ->page($page, $pagesize)
            ->order('m.create_time', 'desc')
            ->select();

        return $data ? $data->toArray() : [];
    }

    /**
     * 会员列表统计
     * @param $where
     * @return mixed
     */
    public static function countUsersMemberList($where)
    {
        return self::alias('m')
            ->join('users u', 'm.uid = u.id', 'left')
            ->where($where)
            ->where('m.end_time', '>', time())
            ->count();
    }

    /**
     * 失效会员统计
     * @param array $where
     * @return int
     */
    public static function countInvalidUsersMember($where = [])
    {
        return self::where($where)
            ->where('end_time', '<=', time())
            ->count();
    }

    /**
     * 会员用户数据
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function findUsersMember($where)
    {
        $info = self::where($where)->find();
        return $info ? $info->toArray() : [];
    }

    /**
     * 更新会员用户信息
     * @param $data
     * @param $where
     * @return UsersMember
     */
    public static function updateUsersMember($data, $where)
    {
        return self::update($data, $where);
    }

    /**
     * 创建会员用户信息
     * @param $data
     * @return UsersMember|Model
     */
    public static function createUsersMember($data)
    {
        return self::create($data);
    }

    /**
     * 本月会员数据
     * @param $where
     * @return array
     */
    public static function getUsersMemberMonth($where = [])
    {
        $data = self::fieldRaw("COUNT(*) total,create_time")
            ->whereMonth('create_time')
            ->where($where)
            ->where('end_time', '>', time())
            ->group('create_time')
            ->select();
        return $data ? $data->toArray() : [];
    }

    /**
     * 本月会员数据1
     * @param $where
     * @param $startTime
     * @param $endTime
     * @return array
     */
    public static function getUsersMemberMonth1($where, $startTime, $endTime)
    {
        $data = self::fieldRaw("COUNT(*) total,create_time")
            ->where($where)
            ->whereTime('create_time', '>=', $startTime)
            ->whereTime('create_time', '<', $endTime)
            ->where('end_time', '>', time())
            ->group('create_time')
            ->select();

        return $data ? $data->toArray() : [];
    }

    /**
     * 本周会员总数
     * @param $where
     * @return int
     */
    public static function countUsersMemberWeek($where)
    {
        return self::where($where)
            ->whereWeek('create_time')
            ->where('end_time', '>', time())
            ->count();
    }

    /**
     * 本月会员总数
     * @param $where
     * @return int
     */
    public static function countUsersMemberMonth($where)
    {
        return self::where($where)
            ->whereMonth('create_time')
            ->where('end_time', '>', time())
            ->count();
    }
}